package evolutionaryLoop.selectionMechanisme;

import evolutionaryLoop.Population;
import evolutionaryProblems.Phenotype;

public class Tournament extends LocalSelectionMechanisme{
	
	private double epsilon;  
	private int tournamentSize; 
	private Population population; 
	private SelectionMechanism tournamentSelectionMechanism; 
	
	public Tournament(double epsilon, int tournamentSize, SelectionMechanism tournamentSelectionMechanism){
		this.epsilon = epsilon; 
		this.tournamentSize = tournamentSize; 
		this.tournamentSelectionMechanism = tournamentSelectionMechanism; 
	}

	@Override
	public Phenotype select() {
		if (Math.random() < epsilon){
			return population.getRandom(); 
		}
		Population tournamentCompetitors = population.getRandomPopulation(tournamentSize); 
		tournamentSelectionMechanism.setPopulation(tournamentCompetitors); 
		return tournamentSelectionMechanism.select();
	}

	@Override
	public void setPopulation(Population population) {
		this.population = population; 
	}

	public String toString(){
		return "Tournament selection";
	}
	
}
